<?php
/**
 * 微信模板消息
 * @App	
 * @Group 
 * @Action WeChatTemplateMsg
 * @author lilei
 * @date 2018年8月20日
 *
 */
namespace app\common\service\wechat;
use app\common\model\article\PublishArticleCoinModel;
use app\common\model\article\PublishArticleModel;
use app\common\service\UserLoginOnVerificationConfig;

class WeChatTemplateMsg extends WeChatService {
	
	/**
	 * wechat操作句柄
	 * @var object | \WeChat\Template
	 */
	private $handle;
	
	/**
	 * 默认模板编号
	 * @var string
	 */
	public static $TemplateID = '';
	
	
	/**
	 * 初始化方法
	 * {@inheritDoc}
	 * @see WeChatService::__init()
	 */
	public function __init() {
		$this->handle = new \WeChat\Template(self::$option);
		self::$TemplateID = self::$WxConfig['TemplateID'];
	}
	
	/**
	 * 获取所有模板列表
	 * @return array
	 */
	public function getTemplateList() {
		$res = $this->handle->getAllPrivateTemplate();
		return $res?:array();
	}
	
	/**
	 * 发送模板消息
	 * @param string $openID	发送用户的OpenID
	 * @param string $url		跳转的URL
	 * @param string $content	详细内容
	 * @param string $remark	备注
	 * @param string $symbol	币种的Symbol
	 * @param string $project	项目名称
	 * @param string $tplID		模板编号
	 * @return bool
	 */
//	public function sendTemplateMsg( $openID, $url, $content, $symbol, $remark, $project='HOLD持仓资讯' , $tplID='' ) {
//		$assign = array(
//				'first' => array('value'=>$content, 'color'=>'#2846CD'),
//				'keyword1' => array('value' => $project),
//				'keyword2' => array('value' => $symbol, 'color'=>'#2846CD'),
//				'keyword3' => array('value' => date("Y-m-d H:i:s")),
//				'remark'   => array('value' => "【摘要】".$remark."\r\n\r\n点击查看详情 ↓"),
//		);
//
//		$data = [
//				'touser' => $openID,
//				'template_id' => $tplID ?:self::$TemplateID,
//				'url' => $url,
//				'data' => $assign,
//		];
//
//		$a = new WeChat\Template(self::$option);
//		$res = $a->send($data);
//		return true;
//	}
    public function sendTemplateMsg( $openID, $url, $content, $symbol, $remark, $project='快讯' , $tplID='' ) {
        $keyword1 = $symbol ?  $symbol.' 最新资讯' : "币圈热点";
        $assign = array(
            'first' => array('value'=>$content."\r\n", 'color'=>'#2846CD'),
            'keyword1' => array('value' => $keyword1, 'color'=>'#2846CD'),
            'keyword2' => array('value' => $remark),
            'keyword3' => array('value' => date("Y-m-d H:i:s")),
            'remark'   => array('value' => $project),
        );

        $data = [
            'touser' => $openID,
            'template_id' => $tplID ?:self::$TemplateID,
            'url' => $this->getWeChatSendUrl( $url, $openID ),
            'data' => $assign,
        ];

        $a = new \WeChat\Template(self::$option);
        $a->send($data);
        return true;
    }


    public function send_wechat( $openID ) {
        $assign = array(
            'first' => array('value'=>"致歉：11月23日18点30分出现价格预警服务异常。"."\r\n", 'color'=>'#2846CD'),
            'keyword1' => array('value' => "公告", 'color'=>'#2846CD'),
            'keyword2' => array('value' => "11月23日18点30分，部分HOLD用户收到“价格预警”的错误消息推送，经查明系行情数据服务升级过程中导致的Bug，目前服务已恢复正常。对您造成的打扰深感抱歉。"),
            'keyword3' => array('value' => date("Y-m-d H:i:s")),
            'remark'   => array('value' => "\r\n点击查看全文 ↓"),
        );

        $data = [
            'touser' => $openID,
            'template_id' => self::$TemplateID,
            'url' => $this->getWeChatSendUrl( "https://ihold.com/detail.html?id=36277", $openID ),
            'data' => $assign,
        ];

        $a = new \WeChat\Template(self::$option);
        $res = $a->send($data);
        return $res;
    }

    /**
     * 推送价格异动
     */
    public function sendTempCoinChangeMsg( $openID, $url, $symbol, $data , $tplID='' ){
        $remark   = "\r\n点击查看【{$symbol}】币种详情和实时资讯";

        // 新版测试
        if ( ENV == "release" && in_array( $openID, UserLoginOnVerificationConfig::$open_id_config )){
            $publishArticleModel = PublishArticleModel::getInstance();
            $artCoinModel = PublishArticleCoinModel::getInstance();
            $coin_art_list = $artCoinModel->get_coin_article_list($data['coin_id']);
            $remark = "\r\n";
            if ( $coin_art_list ) {
            	$where = array(
            			'create_time' => array(
            					'between', array( date('Y-m-d H:i:s', time() - 86400), date('Y-m-d H:i:s' ) )
            			),
            			'deleted' => -1,
            			'is_published' => 1,
            			'is_tmp' => -1,
            			'id' => array('in', $coin_art_list),
            	);
            	$list = $publishArticleModel->where($where)->order('id desc')->limit(2)->select();
            	if ( $list ){
            		$remark = "\r\n【{$symbol}】最新重要资讯\r\n";
            		foreach ( $list as $value ){
            			$time = date( "m月d日",strtotime($value['create_time']));
            			$remark .= $value['title']." ({$time})"."\r\n\r\n";
            		}
            		
            		$remark = rtrim( $remark, "\r\n\r\n");
            	}
            	
            	
            	//        $remark .= "\r\n\r\n点击查看【{$symbol}】详情和实时资讯 ↓";
            }
        }


        $assign = array(
            'first' => array('value'=> $data['message']."\r\n", 'color'=>$data['color'] == 1 ? '#418F3C' :'#C13833' ),
            'keyword1' => array('value'=>"【{$symbol}】币种价格波动", 'color'=> '#2846CD'),
//            'keyword2' => array('value'=>"【{$symbol}】", 'color'=> '#2846CD' ),
            'keyword2' => array('value' => date("Y-m-d H:i:s")),
            'remark'   => array('value' => $remark),
        );

        $data = [
            'touser' => $openID,
            'template_id' => $tplID ?:self::$TemplateID,
            'url' => $this->getWeChatSendUrl( $url, $openID ),
            'data' => $assign,
        ];
        try{
            $a = new \WeChat\Template(self::$option);
            $res = $a->send($data);
        } catch ( \Exception $exception ){
            echo 'error'.PHP_EOL;
            return true;
        }

        return $res;
    }

    private function getWeChatSendUrl( $url , $open_id )
    {
        return $url.'&isWechatOfficial=1&openId='.$open_id;
    }
	
	
	
	
}